Skip to content

Method: KopfundZahlPlayerImpl(String, Map)

1: package de.fhdw.gaming.ipspiel22.kopfundzahl.domain.impl;
2:
3: import java.util.Collections;
4: import java.util.LinkedHashMap;
5: import java.util.Map;
6: import java.util.Objects;
7: import java.util.Optional;
8:
9: import de.fhdw.gaming.core.domain.AbstractPlayer;
10: import de.fhdw.gaming.ipspiel22.kopfundzahl.domain.KopfundZahlPlayer;
11:
12: /**
13: * Implements {@link KopfundZahlPlayer}.
14: */
15: public final class KopfundZahlPlayerImpl extends AbstractPlayer<KopfundZahlPlayer> implements KopfundZahlPlayer {
16:
17: /**
18: * The possible outcomes of this player. The key for the first-level map is the answer of the first player, the key
19: * for the second-level map is the answer of the second player.
20: */
21: private final Map<Boolean, Map<Boolean, Double>> possibleOutcomes;
22: /**
23: * The answer of the player.
24: */
25: private Optional<Boolean> answer;
26:
27: /**
28: * Creates a KopfundZahl player.
29: *
30: * @param name The name of the player.
31: * @param possibleOutcomes The possible outcomes of this player. The key for the first-level map is the answer of
32: * the first player, the key for the second-level map is the answer of the second player.
33: */
34: public KopfundZahlPlayerImpl(final String name, final Map<Boolean, Map<Boolean, Double>> possibleOutcomes) {
35: super(name);
36: this.possibleOutcomes = Collections.unmodifiableMap(
37: new LinkedHashMap<>(Objects.requireNonNull(possibleOutcomes, "possibleOutcomes")));
38: this.answer = Optional.empty();
39: }
40:
41: /**
42: * Creates a KopfundZahl player.
43: *
44: * @param source The {@link KopfundZahlPlayer} to copy.
45: */
46: KopfundZahlPlayerImpl(final KopfundZahlPlayer source) {
47: super(source);
48: this.possibleOutcomes = source.getPossibleOutcomes();
49: this.answer = source.getAnswer();
50: }
51:
52: @Override
53: public String toString() {
54: return String
55: .format("KopfundZahlPlayer[name=%s, state=%s, outcome=%s, answer=%s]", this.getName(),
56: this.getState(), this.getOutcome(), this.answer);
57: }
58:
59: @Override
60: public boolean equals(final Object obj) {
61: if (obj instanceof KopfundZahlPlayerImpl) {
62: final KopfundZahlPlayerImpl other = (KopfundZahlPlayerImpl) obj;
63: return super.equals(obj) && this.answer.equals(other.answer);
64: }
65: return false;
66: }
67:
68: @SuppressWarnings("PMD.UselessOverriding")
69: @Override
70: public int hashCode() {
71: return super.hashCode();
72: }
73:
74: @Override
75: public Map<Boolean, Map<Boolean, Double>> getPossibleOutcomes() {
76: return this.possibleOutcomes;
77: }
78:
79: @Override
80: public Optional<Boolean> getAnswer() {
81: return this.answer;
82: }
83:
84: @Override
85: public void setAnswer(final boolean newAnswer) {
86: if (this.answer.isPresent()) {
87: throw new IllegalStateException(String.format("Player %s tried to change her answer.", this.getName()));
88: }
89: this.answer = Optional.of(newAnswer);
90: }
91:
92: @Override
93: public KopfundZahlPlayer deepCopy() {
94: return new KopfundZahlPlayerImpl(this);
95: }
96: }